$binFolder = (resolve-path ..\bin)
$results = [System.IO.File]::Exists("$binFolder\SmartClientApp\Shell.exe")
if ($results -eq $true)
{ 
		$solutionFile = (resolve-path "..\solution\LitwareHR-SC\LitwareHR.VS2008.sln")
		$projectFile = (resolve-path "..\solution\LitwareHR-SC\Source\Infrastructure\Shell\Shell.csproj")

		$SCSolutionFolder = (resolve-path "..\solution\LitwareHR-SC")

		if ((test-path "$SCSolutionFolder\bin\Release\App.Publish\") -eq $true)
		{
		   write "Removing previous clickonce package..."
		  remove-item "$SCSolutionFolder\bin\Release\App.Publish\" -force -recurse
		}

		copy $projectFile "$projectFile.original"

		$doc = new-object System.Xml.XmlDocument
		$doc.Load($projectFile)

		$nsmgr = new-object System.Xml.XmlNamespaceManager($doc.get_NameTable())
		$nsmgr.AddNamespace("prj","http://schemas.microsoft.com/developer/msbuild/2003")

		#Update existing nodes
		$generateManifestsNode = $doc.SelectSingleNode("/prj:Project/prj:PropertyGroup/prj:GenerateManifests",$nsmgr)
		$generateManifestsNode.set_InnerText("true")

		$signManifestsNode = $doc.SelectSingleNode("/prj:Project/prj:PropertyGroup/prj:SignManifests",$nsmgr)
		$signManifestsNode.set_InnerText("true")

		$publishUrlNode = $doc.SelectSingleNode("/prj:Project/prj:PropertyGroup/prj:PublishUrl",$nsmgr)
		$publishUrlNode.set_InnerText("http://www.litware.com/LitwareHRDeployment/")

		$publishUrlNode = $doc.SelectSingleNode("/prj:Project/prj:PropertyGroup/prj:ProductName",$nsmgr)
		$publishUrlNode.set_InnerText("LitwareHR Smart Client Sample Application")

		$publishUrlNode = $doc.SelectSingleNode("/prj:Project/prj:PropertyGroup/prj:PublisherName",$nsmgr)
		$publishUrlNode.set_InnerText("Microsoft Corporation")

		#Create nodes under PropertyGroup
		$propertyGroupNode = $doc.SelectSingleNode("/prj:Project/prj:PropertyGroup", $nsmgr)
		$applicationVersionNode = $propertyGroupNode.SelectSingleNode("/prj:ApplicationVersion", $nsmgr)

		$applicationRevisionNode = $doc.CreateElement("ApplicationRevision", "http://schemas.microsoft.com/developer/msbuild/2003")
		$applicationRevisionNode.set_InnerText(0)
		$propertyGroupNode.InsertBefore($applicationRevisionNode, $applicationVersionNode) | out-null

		$useApplicationTrustNode = $doc.CreateElement("UseApplicationTrust", "http://schemas.microsoft.com/developer/msbuild/2003")
		$useApplicationTrustNode.set_InnerText("false")
		$propertyGroupNode.InsertBefore($useApplicationTrustNode, $applicationVersionNode) | out-null

		#Append content items for deployment that are on the project root as link
		$profileCatalogContentNode = $doc.SelectSingleNode("/prj:Project/prj:ItemGroup/prj:Content[@Include='ProfileCatalog.xml']",$nsmgr)

		$firstItemGroupNode = $profileCatalogContentNode.get_ParentNode()

		$filesToAdd = Get-Content (resolve-path .\tools\rootDependencies.txt)

		foreach($filePath in $filesToAdd)
		{
			$lastDashLocation = ([System.String]$filePath).LastIndexOf("\")
			$fileName = ([System.String]$filePath).Substring($lastDashLocation+1)
			
			$contentNode = $doc.CreateElement("Content", "http://schemas.microsoft.com/developer/msbuild/2003");
			$contentNode.SetAttribute("Include", $filePath)

			$linkElement = $doc.CreateElement("Link", "http://schemas.microsoft.com/developer/msbuild/2003");
			$linkElement.set_InnerText($fileName)
			$contentNode.AppendChild($linkElement) | out-null

			$copyToOutputDirectoryElement = $doc.CreateElement("CopyToOutputDirectory", "http://schemas.microsoft.com/developer/msbuild/2003");
			$copyToOutputDirectoryElement.set_InnerText("PreserveNewest")
			$contentNode.AppendChild($copyToOutputDirectoryElement) | out-null

			$firstItemGroupNode.AppendChild($contentNode) | out-null
		}

		#Append content items for deployment that are on the project as link
		$noneNode = $doc.SelectSingleNode("/prj:Project/prj:ItemGroup/prj:None[@Include='App.config']",$nsmgr)

		$secondItemGroupNode = $noneNode.get_ParentNode()

		$filesToAdd = Get-Content (resolve-path .\tools\imagesDependencies.txt)

		foreach($filePath in $filesToAdd)
		{
			$lastDashLocation = ([System.String]$filePath).LastIndexOf("Release\")
			$fileName = ([System.String]$filePath).Substring($lastDashLocation+8)
			
			$contentNode = $doc.CreateElement("Content", "http://schemas.microsoft.com/developer/msbuild/2003");
			$contentNode.SetAttribute("Include", $filePath)

			$linkElement = $doc.CreateElement("Link", "http://schemas.microsoft.com/developer/msbuild/2003");
			$linkElement.set_InnerText($fileName)
			$contentNode.AppendChild($linkElement) | out-null

			$copyToOutputDirectoryElement = $doc.CreateElement("CopyToOutputDirectory", "http://schemas.microsoft.com/developer/msbuild/2003");
			$copyToOutputDirectoryElement.set_InnerText("PreserveNewest")
			$contentNode.AppendChild($copyToOutputDirectoryElement) | out-null

			$secondItemGroupNode.AppendChild($contentNode) | out-null
		}

		#Add files to publish
		$profileCatalogNode = $doc.SelectSingleNode("/prj:Project/prj:ItemGroup/prj:PublishFile[@Include='ProfileCatalog.xml']",$nsmgr)

		$thirdItemGroupNode = $profileCatalogNode.get_ParentNode()
		$filesToPublish = "LitwareHR.sdf", "RolesIcon.xml"

		foreach($file in $filesToPublish)
		{
			$publishFileNode = $doc.CreateElement("PublishFile", "http://schemas.microsoft.com/developer/msbuild/2003");
			$publishFileNode.SetAttribute("Include", $file)

			$visibleElement = $doc.CreateElement("Visible", "http://schemas.microsoft.com/developer/msbuild/2003");
			$visibleElement.set_InnerText($false)
			$publishFileNode.AppendChild($visibleElement) | out-null

			$groupElement = $doc.CreateElement("Group", "http://schemas.microsoft.com/developer/msbuild/2003");
			$publishFileNode.AppendChild($groupElement) | out-null

			$targetPathElement = $doc.CreateElement("TargetPath", "http://schemas.microsoft.com/developer/msbuild/2003");
			$publishFileNode.AppendChild($targetPathElement) | out-null

			$publishStateElement = $doc.CreateElement("PublishState", "http://schemas.microsoft.com/developer/msbuild/2003");
			$publishStateElement.set_InnerText("Include")
			$publishFileNode.AppendChild($publishStateElement) | out-null

			$fileTypeElement = $doc.CreateElement("FileType", "http://schemas.microsoft.com/developer/msbuild/2003");
			$fileTypeElement.set_InnerText("File")
			$publishFileNode.AppendChild($fileTypeElement) | out-null
			
			$thirdItemGroupNode.AppendChild($publishFileNode) | out-null
		}

		& attrib -R $projectFile

		$doc.Save($projectFile)

		write "Building Smart Client Publish Package..."
		& $env:windir\Microsoft.Net\Framework\v3.5\MSBuild.exe $solutionFile /p:Configuration=Release /target:publish | out-null
		write "Smart Client Publish Package Build Complete"
		$sourcePath = (resolve-path "..\solution\LitwareHR-SC\bin\Release\app.publish\")

		$destinationPath = (resolve-path "..\bin\Litware.WebSite")

		if ( (test-path "$destinationPath\LitwareHRDeployment\") -eq $false)
		{
		   mkdir "$destinationPath\LitwareHRDeployment"
		}

		xcopy "$sourcePath*.*" "$destinationPath\LitwareHRDeployment" /E /Y /R /Q /I

		copy "$projectFile.original" $projectFile 
		del "$projectFile.original" -force		
		
		write "Adding link to Clickonce Solution in LitwareWebSite..."
		$siteMapDoc = new-object System.Xml.XmlDocument
		$siteMapDoc.Load("$destinationPath\Web.sitemap")
		
		$siteMapNode = $siteMapDoc.SelectSingleNode("//*[local-name()='siteMapNode' and namespace-uri()='http://schemas.microsoft.com/AspNet/SiteMap-File-1.0'][@title='Home']")		
		$contentNode = $siteMapDoc.CreateElement("siteMapNode", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0");
		$contentNode.SetAttribute("url", "http://www.litware.com/LitwareHRDeployment/shell.application")
		$contentNode.SetAttribute("title", "Download Litware Smart Client")
		$contentNode.SetAttribute("description", "Context")
		$siteMapNode.AppendChild($contentNode)
		$siteMapDoc.Save("$destinationPath\Web.sitemap")
		
    
}